#!/usr/bin/env python
# coding: utf-8

from urllib.parse import urlparse,urljoin,quote,quote_plus,urlencode

'''
urlparse()：
对一个url的各组成部分进行拆解
'''
url = "https://gitlab.weyeenet.com:4443/weyee/msg-sender/tree/master?a=1&b=中文"
parseResult = urlparse(url)
print(parseResult) #ParseResult(scheme='https', netloc='gitlab.weyeenet.com:4443', path='/weyee/msg-sender/tree/master', params='', query='a=1&b=中文', fragment='')
print(parseResult.scheme)   #https
print(parseResult.netloc)   #gitlab.weyeenet.com:4443
print(parseResult.path)     #/weyee/msg-sender/tree/master
print(parseResult.query)    #a=1&b=中文
print(parseResult.port)     #4443
print(parseResult.geturl()) #https://gitlab.weyeenet.com:4443/weyee/msg-sender/tree/master?a=1&b=中文

'''
urljoin():
用url来替换base的对应部分
'''
base = "http://www.weyeenet.net/items/list"
url = "getitems/10"
print(urljoin(base, url)) #http://www.weyeenet.net/items/getitems/10

'''
quote()
对任意字符串进行url转义， 但不对’/‘转义
'''
querystr="?a=中文&b=/正则/"
print(quote(querystr))   #%3Fa%3D%E4%B8%AD%E6%96%87%26b%3D/%E6%AD%A3%E5%88%99/
html="<a href='a.b.com'>hahahahaha</a>"
print(quote(html))  #%3Ca%20href%3D%27a.b.com%27%3Ehahahahaha%3C/a%3E

'''
quote_plus()
对任意字符串进行url转义， 包括’/‘
'''
querystr="?a=中文&b=/正则/"
print(quote_plus(querystr))  #%3Fa%3D%E4%B8%AD%E6%96%87%26b%3D%2F%E6%AD%A3%E5%88%99%2F

'''
urlencode()
'''
query = {
    "a": "中文",
    "b": "/正则/"
}
print(urlencode(query))    #a=%E4%B8%AD%E6%96%87&b=%2F%E6%AD%A3%E5%88%99%2F